package it.eng.area118.mdocommon.android.network;

import java.util.Calendar;

import it.eng.area118.mdocommon.android.log.BasicLogger;
import it.eng.area118.mdocommon.android.model.AndroidModelContainer;
import it.eng.area118.mdocommon.android.util.DateUtils;
import it.eng.area118.mdocommon.android.util.SystemTime;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class TimeTickReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		BasicLogger.getLog().info("TimeTickReceiver onReceive: ....");
		SystemTime currentTime = (SystemTime) AndroidModelContainer
				.getInstance().getModel(SystemTime.class.toString());
		if (currentTime != null) {
			BasicLogger.getLog().info("TimeTickReceiver currentTime != null ....");
			updateTimeTick(currentTime);
		} else {			
			BasicLogger.getLog().info("TimeTickReceiver currentTime == null ....");
			currentTime = DateUtils.cal2sys(Calendar.getInstance());
			AndroidModelContainer.getInstance().putModel(
					SystemTime.class.toString(), currentTime, true);
		}

	}

	public synchronized void updateTimeTick(SystemTime currentTime) {
		BasicLogger.getLog().info("TimeTickReceiver updateTimeTick");
		currentTime.setMinutes((short) (currentTime.getMinutes() + 1));
		AndroidModelContainer.getInstance().putModel(
				SystemTime.class.toString(), currentTime, true);
	}

}
